import json
from tool.cleanbyid import CleanById
from flask import Flask, jsonify, request
from flask_cors import CORS
from service.train_service import TrainService
from service.train_list_service import TrainListService

# configuration
DEBUG = True

# instantiate the app
app = Flask(__name__, static_url_path='/static', static_folder='../../')
app.config.from_object(__name__)

# enable CORS
CORS(app, resources={r'/*': {'origins': '*'}})


# sanity check route
@app.route('/open', methods=['GET'])
def open_door():
    return jsonify(u'芝麻开门！')


@app.route('/train/<train_id>')
def train(train_id: int):
    print("train")
    print(type(train_id))
    service = TrainService()
    dto = service.setDTO(int(train_id))
    json_dao = json.dumps(dto, default=lambda o: o.__dict__, ensure_ascii=False)
    return json_dao
@app.route('/clean/<train_id>')
def clean(train_id: str):
    print("clean"+train_id)
    CleanById().clean(int(train_id))
    return jsonify("ok")
@app.route('/')
def train_view():
    service = TrainListService()
    print("train_view")
    tlist = service.get_train_list()
    json_tlist = json.dumps(tlist, default=lambda o: o.__dict__, ensure_ascii=False)
    return json_tlist


if __name__ == '__main__':
    app.config['JSON_AS_ASCII'] = False
    app.run()
